/**
 * Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package work.konbai.basic.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.ObjectUtil;
import org.springframework.web.bind.annotation.*;
import work.konbai.basic.entity.CourseStudent;
import work.konbai.basic.entity.Student;
import work.konbai.basic.service.ICourseStudentService;
import work.konbai.basic.service.IStudentService;

import javax.validation.Valid;

/**
 * 学员信息表 控制器
 *
 * @author KonBAI
 * @since 2020-12-28
 */
@RestController
@AllArgsConstructor
@RequestMapping("/student")
@Api(value = "学员信息表", tags = "学员信息表接口")
public class StudentController extends BladeController {

	private final IStudentService studentService;

	private final ICourseStudentService courseStudentService;

	/**
	* 详情
	*/
	@GetMapping("/detail")
    @ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入student")
	public R<Student> detail(Student student) {
		return R.data(studentService.getOne(Condition.getQueryWrapper(student)));
	}

	/**
	* 分页 学员信息表
	*/
	@GetMapping("/list")
    @ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入student")
	public R<IPage<Student>> list(Student student, Query query) {
		IPage<Student> pages = studentService.page(Condition.getPage(query), Condition.<Student>getLambdaQueryWrapper()
			.like(ObjectUtil.isNotEmpty(student.getRealName()), Student::getRealName, student.getRealName()));
		return R.data(pages);
	}

	/**
	* 新增 学员信息表
	*/
	@PostMapping("/save")
    @ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入student")
	public R save(@Valid @RequestBody Student student) {
		return R.status(studentService.save(student));
	}

	/**
	* 修改 学员信息表
	*/
	@PostMapping("/update")
    @ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入student")
	public R update(@Valid @RequestBody Student student) {
		return R.status(studentService.updateById(student));
	}

	/**
	* 新增或修改 学员信息表
	*/
	@PostMapping("/submit")
    @ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入student")
	public R submit(@Valid @RequestBody Student student) {
		return R.status(studentService.saveOrUpdate(student));
	}

	/**
	* 删除 学员信息表
	*/
	@PostMapping("/remove")
    @ApiOperationSupport(order = 7)
	@ApiOperation(value = "删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(studentService.removeByIds(Func.toLongList(ids)));
	}

	/**
	 * 分配课程
	 */
	@PostMapping("/assign")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "分配课程", notes = "传入courseStudent")
	public R assignCourse(@Valid @RequestBody CourseStudent courseStudent) {
		int count = courseStudentService.lambdaQuery()
			.eq(CourseStudent::getStudentId, courseStudent.getStudentId())
			.eq(CourseStudent::getCourseId, courseStudent.getCourseId())
			.count();
		if (SqlHelper.retBool(count)) {
			return R.success("该学生已分配此课程");
		}
		return R.status(courseStudentService.save(courseStudent));
	}

	/**
	 * 移除课程
	 */
	@PostMapping("/exclude")
	@ApiOperationSupport(order = 9)
	@ApiOperation(value = "移除课程", notes = "传入id")
	public R removeCourse(@ApiParam(value = "主键", required = true) @RequestParam String id) {
		return R.status(courseStudentService.removeById(id));
	}
}
